;; pentru a verifica daca combinatiile de mancare sunt corecte facem un produs cartezian partial , din lista de mancaruri care nu se pot combina
;; vedem daca exista intrun meniu cel putin una dintre ele , daca da atunci meniul nu este ok

(deffunction inter(?list_content1 ?list_content2) 
	(bind ?x true)
	(if (>= (length$ (intersection$ ?list_content1 ?list_content2)) 2)
		then (bind ?x false))
	
	return ?x
)

(defrule check_menu_if_is_not_correct "check if menu is not correct"
	(run_check_meniu true)
	?food_item0_position<-(meniu_food_item0 ?food_item_name0)
	?food_item1_position<-(meniu_food_item1 ?food_item_name1)
	?food_item2_position<-(meniu_food_item2 ?food_item_name2)
	(bad_food_combination $?list_content0)
	?stop<-(stop_execution false)
	=>
	(bind ?menu_list (create$ ?food_item_name0 ?food_item_name1 ?food_item_name2))
	(bind ?is_ok (inter $?list_content0 ?menu_list))
	(bind ?menu_status ?is_ok)
	(if (eq ?is_ok false) then
		(bind ?menu_status false)
		(retract ?stop)
		(assert (stop_execution true)))
	(assert (is_menu_ok ?menu_status))
	(store P ?menu_status)
)
	

(defrule valid_food_item  "check if a food item is ok to be part of a menu"
	(valid_if_food_item_is_part_of_menu true)
	?food_item0_position<-(meniu_food_item0 ?food_item_name0)
	?food_item1_position<-(meniu_food_item1 ?food_item_name1)
	?food_item2_position<-(meniu_food_item2 ?food_item_name2)
	?food_item3_position<-(meniu_food_item3 ?food_item_name3)
	(bad_food_combination $?list_content0)
	?stop<-(stop_execution false)
	=>
	(bind ?menu_list (create$ ?food_item_name0 ?food_item_name1 ?food_item_name2 ?food_item_name3))
	(bind ?is_ok (inter $?list_content0 ?menu_list))
	(bind ?menu_status ?is_ok)
	(if (eq ?is_ok false) then
		(bind ?menu_status false)
		(retract ?stop)
		(assert (stop_execution true)))
	(assert (is_menu_ok ?menu_status))
	(store P ?menu_status)
)
	
;--------------------------------------------------------------------------------------------------------------------------------------------------------------------------

;-------------------------------------- check if one particular food item is good or bad for diabetes ---------------------------------------------------------------------

(defrule check_if_is_not_ok_for_diabetes1 "is not ok for diabetes"
	(deasese diabetes); have diabetes
	(food_item_to_check ?food_param)
	(bad_food_item_diabetes ?food_param)
	=>
	(assert (is_recomanded false))
	(store M false)
)

(defrule check_if_is_ok_for_diabetes1 "is ok for diabetes"
	(deasese diabetes); have diabetes
	(food_item_to_check ?food_param)
	(good_food_item_diabetes ?food_param)
	=>
	(assert (is_recomanded true))
	(store M true)
)

(defrule is_ok_for_diabetes_but_is_not_in_the_recom_list "a food item that is not in the bad food for diabetes and is not in the good food for diabetes list"
	(deasese diabetes); have diabetes
	(food_item_to_check ?food_param)
	(not (good_food_item_diabetes ?food_param))
	(not (bad_food_item_diabetes ?food_param))
	=>
	(assert (is_recomanded true))
	(store M true)
)	

;-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; check if is  ok recomand a food item for diabetes

(defrule recomand_diabetes_food_item "recomad a diabetic food"
	(deasese diabetes); have diabetes
	(prefference_item Cucumber)
	(not(recomanded_before Cucumber))
	(not(meniu_food_item Cucumber))
	=>
	(assert (recomandated_food_item Cucumber))
	(printout t '111111111111a ' crlf)
	(store REC_FOOD Cucumber)
)

(defrule recomand_diabetes_food_item1 "recomad a diabetic food"
	(deasese diabetes); have diabetes
	(prefference_item Cabbage)
	(not(recomanded_before Cabbage))
	(not(meniu_food_item Cabbage))
	=>
	(assert (recomandated_food_item Cabbage))
	(printout t '2222222222a ' crlf)
	(store REC_FOOD Cabbage)
)

(defrule recomand_diabetes_food_item2 "recomad a diabetic food"
	(deasese diabetes); have diabetes
	(prefference_item Milk)
	(not(recomanded_before Milk))
	(not(meniu_food_item Milk))
	=>
	(assert (recomandated_food_item Milk))
	(printout t 333a ' crlf)	
	(store REC_FOOD Milk)
)

(defrule recomand_diabetes_food_item3 "recomad a diabetic food"
	(deasese diabetes); have diabetes
	(prefference_item Brown_rice)
	(not(recomanded_before Brown_rice))
	(not(meniu_food_item Brown_rice))
	=>
	(printout t '4444a ' crlf)
	(assert (recomandated_food_item Brown_rice))
	(store REC_FOOD Brown_rice)
)
	
(defrule recomand_diabetes_food_item4 "recomad a diabetic food"
	(deasese diabetes); have diabetes
	(prefference_item Fish)
	(not(recomanded_before Fish))	
	(not(meniu_food_item Fish))
	=>
	(printout t '5555555a ' crlf)
	(assert (recomandated_food_item Fish))
	(store REC_FOOD Fish)
)
	
(defrule recomand_diabetes_food_item5 "recomad a diabetic food"
	(deasese diabetes); have diabetes
	(prefference_item Tofu)
	(not(recomanded_before Tofu))	
	(not(meniu_food_item Tofu))
	=>
	(printout t '6666666a ' crlf)	
	(assert (recomandated_food_item Tofu))
	(store REC_FOOD Tofu)
)

(defrule recomand_diabetes_food_item6 "recomad a diabetic food"
	(deasese diabetes); have diabetes
	(prefference_item Low_fat_yogurt)
	(not(recomanded_before Low_fat_yogurt))	
	(not(meniu_food_item Low_fat_yogurt))
	=>
	(printout t '77777777a ' crlf)
	(assert (recomandated_food_item Low_fat_yogurt))
	(store REC_FOOD Low_fat_yogurt)
)

(defrule recomand_diabetes_food_item7 "recomad a diabetic food"
	(deasese diabetes); have diabetes
	(prefference_item Tea)
	(not(recomanded_before Tea))
	(not(meniu_food_item Tea))
	=>
	(printout t '88888888a ' crlf)
	(assert (recomandated_food_item Tea))
	(store REC_FOOD Tea)
)

(defrule recomand_diabetes_food_item8 "recomad a diabetic food"
	(deasese diabetes); have diabetes
	(prefference_item Nuts)
	(not(recomanded_before Nuts))	
	(not(meniu_food_item Nuts))
	=>
	(printout t '9999999a ' crlf)
	(assert (recomandated_food_item Nuts))
	(store REC_FOOD Nuts)
)

;;;; if in the prefference list doesn't find a good food item for diabetes 
;;;; then recomand one form our list

(defrule recomand_default_food_item  "recomant a default food item"
	(deasese diabetes);
	(not(prefference_item Cucumber))
	(not(prefference_item Cabbage))
	(not(prefference_item Milk))
	(not(prefference_item Brown_rice))
	(not(prefference_item Baked_potatoes))
	(not(prefference_item Fish))
	(not(prefference_item Tofu))
	(not(prefference_item Low_fat_yogurt))
	(not(prefference_item Tea))
	(not(prefference_item Nuts))
	(not(recomanded_before Cucumber))
	(not(meniu_food_item Cucumber))
	=>
	(printout t '1111b ' crlf)	
	(assert (recomandated_food_item Cucumber))
	(store REC_FOOD Cucumber)
)


(defrule recomand_default_food_item1  "recomant a default food item"
	(deasese diabetes);
	(not(prefference_item Cucumber))
	(not(prefference_item Cabbage))
	(not(prefference_item Milk))
	(not(prefference_item Brown_rice))
	(not(prefference_item Baked_potatoes))
	(not(prefference_item Fish))
	(not(prefference_item Tofu))
	(not(prefference_item Low_fat_yogurt))
	(not(prefference_item Tea))
	(not(prefference_item Nuts))
	(not(recomanded_before Cabbage))
	(not(meniu_food_item Cabbage))
	=>
	(printout t '222b ' crlf)
	(assert (recomandated_food_item Cabbage))
	(store REC_FOOD Cabbage)
)


(defrule recomand_default_food_item2  "recomant a default food item"
	(deasese diabetes);
	(not(prefference_item Cucumber))
	(not(prefference_item Cabbage))
	(not(prefference_item Milk))
	(not(prefference_item Brown_rice))
	(not(prefference_item Baked_potatoes))
	(not(prefference_item Fish))
	(not(prefference_item Tofu))
	(not(prefference_item Low_fat_yogurt))
	(not(prefference_item Tea))
	(not(prefference_item Nuts))
	(not(recomanded_before Milk))
	(not(meniu_food_item Milk))
	=>
	(printout t '333b ' crlf)
	(assert (recomandated_food_item Milk))
	(store REC_FOOD Milk)
)





(defrule recomand_default_food_item4  "recomant a default food item"
	(deasese diabetes);
	(not(prefference_item Cucumber))
	(not(prefference_item Cabbage))
	(not(prefference_item Milk))
	(not(prefference_item Brown_rice))
	(not(prefference_item Baked_potatoes))
	(not(prefference_item Fish))
	(not(prefference_item Tofu))
	(not(prefference_item Low_fat_yogurt))
	(not(prefference_item Tea))
	(not(prefference_item Nuts))
	(not(recomanded_before Baked_potatoes))
	(not(meniu_food_item Baked_potatoes))
	=>
	(printout t '444b ' crlf)
	(assert (recomandated_food_item Baked_potatoes))
	(store REC_FOOD Baked_potatoes)
)


(defrule recomand_default_food_item5  "recomant a default food item"
	(deasese diabetes);
	(not(prefference_item Cucumber))
	(not(prefference_item Cabbage))
	(not(prefference_item Milk))
	(not(prefference_item Brown_rice))
	(not(prefference_item Baked_potatoes))
	(not(prefference_item Fish))
	(not(prefference_item Tofu))
	(not(prefference_item Low_fat_yogurt))
	(not(prefference_item Tea))
	(not(prefference_item Nuts))
	(not(recomanded_before Fish))	
	(not(meniu_food_item Fish))
	=>
	(printout t '5555b ' crlf)
	(assert (recomandated_food_item Fish))
	(store REC_FOOD Fish)
)


(defrule recomand_default_food_item6  "recomant a default food item"
	(deasese diabetes);
	(not(prefference_item Cucumber))
	(not(prefference_item Cabbage))
	(not(prefference_item Milk))
	(not(prefference_item Brown_rice))
	(not(prefference_item Baked_potatoes))
	(not(prefference_item Fish))
	(not(prefference_item Tofu))
	(not(prefference_item Low_fat_yogurt))
	(not(prefference_item Tea))
	(not(prefference_item Nuts))
	(not(recomanded_before Tofu))
	(not(meniu_food_item Tofu))
	=>
	(printout t '666b ' crlf)
	(assert (recomandated_food_item Tofu))
	(store REC_FOOD Tofu)
)


(defrule recomand_default_food_item  "recomant a default food item"
	(deasese diabetes);
	(not(prefference_item Cucumber))
	(not(prefference_item Cabbage))
	(not(prefference_item Milk))
	(not(prefference_item Brown_rice))
	(not(prefference_item Baked_potatoes))
	(not(prefference_item Fish))
	(not(prefference_item Tofu))
	(not(prefference_item Low_fat_yogurt))
	(not(prefference_item Tea))
	(not(prefference_item Nuts))
	(not(recomanded_before Low_fat_yogurt))
	(not(meniu_food_item Low_fat_yogurt))
	=>
	(printout t '777b ' crlf)
	(assert (recomandated_food_item Low_fat_yogurt))
	(store REC_FOOD Low_fat_yogurt)
)


(defrule recomand_default_food_item  "recomant a default food item"
	(deasese diabetes);
	(not(prefference_item Cucumber))
	(not(prefference_item Cabbage))
	(not(prefference_item Milk))
	(not(prefference_item Brown_rice))
	(not(prefference_item Baked_potatoes))
	(not(prefference_item Fish))
	(not(prefference_item Tofu))
	(not(prefference_item Low_fat_yogurt))
	(not(prefference_item Tea))
	(not(prefference_item Nuts))
	(not(recomanded_before Tea))
	(not(meniu_food_item Tea))
	=>
	(printout t '8888b ' crlf)
	(assert (recomandated_food_item Tea))
	(store REC_FOOD Tea)
)


(defrule recomand_default_food_item  "recomant a default food item"
	(deasese diabetes);
	(not(prefference_item Cucumber))
	(not(prefference_item Cabbage))
	(not(prefference_item Milk))
	(not(prefference_item Brown_rice))
	(not(prefference_item Baked_potatoes))
	(not(prefference_item Fish))
	(not(prefference_item Tofu))
	(not(prefference_item Low_fat_yogurt))
	(not(prefference_item Tea))
	(not(prefference_item Nuts))
	(not(recomanded_before Nuts))
	(not(meniu_food_item Nuts))
	=>
	(printout t '9999b ' crlf)
	(assert (recomandated_food_item Nuts))
	(store REC_FOOD Nuts)
)

;; declare a global variable
(defglobal
?*randomize* = (random)
)

;; if the preference is in the food item
;(deffacts default_item_for_diabetes
; (default another)
;  (deasese diabetes)
;) 


(deffunction recomand_random()
	(bind ?rec default)
    (if (< ?*randomize* 6500) then
         (bind ?rec Cucumber)
       else (if (< ?*randomize* 13000) then
                (bind ?rec Cabbage)
             else (if (< ?*randomize* 18500) then
						(bind ?rec Milk)
				   else (if (< ?*randomize* 25000) then
							(bind ?rec Baked_potatoes)
						else (if (< ?*randomize* 31500) then
								(bind ?rec Fish)
							  else (if (< ?*randomize* 38000) then
										(bind ?rec Tofu)
									else (if (< ?*randomize* 44500) then
											(bind ?rec Low_fat_yogurt)
										  else (if (< ?*randomize* 51000) then
													(bind ?rec Tea)
												else (if (< ?*randomize* 57500) then
														(bind ?rec Brown_rice)
												else
												(bind ?rec Nuts))))))))))
	return ?rec
)

	;(printout t "randomize " (recomand_random) crlf)	
	
(defglobal
?*generated_food_item* = (recomand_random)
)
;; daca preferintele sunt deja in meiu generez random din lista de aliemente bune pentru diabetici excluzand cele deja existente in meniu
(defrule recomand_another "recomand another food item"
	(default another)
	(deasese diabetes)
	(not(meniu_food_item ?*generated_food_item*))
	=>
	(assert (recomandated_food_item ?*generated_food_item*))
	(store REC_FOOD ?*generated_food_item*)
)

